package com.example.project_out_in;

import java.util.List;

import Business.bSQL_Heper;
import Entities.EUsers_;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.building_msmart.ulti.Alertdialog_;
import com.building_msmart.ulti.MD5;

public class LoginActivity extends Activity {
	 bSQL_Heper sqlheper;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        sqlheper = new bSQL_Heper(this);
        //Log.d("Count User: ", "Reading .."); 
        long count = sqlheper.countUser();
        //Log.d("Result Count: ", count+""); 
        final MD5 m = new MD5();
        if(count<1)
        {
        	EUsers_ v = new EUsers_();
        	v.setUserName("admin");
        	v.setRole("Quản lý");
        	
        	String pass = m.md5("123456");
        	v.setPassword(pass);
        	sqlheper.addUsers_(v);
        	
        }
        /* Test
         Log.d("Reading: ", "Reading all User.."); 
        List<EUsers_> contacts = sqlheper.getAllUsers_("SELECT  * FROM Users_");       
         
        for (EUsers_ cn : contacts) {
            String log = "Id: "+cn.getID()+" ,Name: " + cn.getUserName() + " ,Pass: " + cn.getPassword()+" ,Chức vụ: " + cn.getRole();
         // Writing Contacts to log
            Log.d("Name: ", log);
        }
         */
        
        final EditText txtUserName = (EditText)findViewById(R.id.txtUserName);
        final EditText txtPass = (EditText)findViewById(R.id.txtPass);
        TextView registerScreen = (TextView) findViewById(R.id.link_to_register);
        Button btnLogin = (Button) findViewById(R.id.btnLogin);
        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				finish();
	            System.exit(0);
			}
		});
        
        btnLogin.setOnClickListener(new OnClickListener() {
			
			@SuppressWarnings("deprecation")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String sql = "SELECT  * FROM Users_ where UserName="+"'"+txtUserName.getText()+"'" +" and Password="+"'"+m.md5(txtPass.getText().toString())+"'";
				//Log.d("SQL: ", sql);
				
				List<EUsers_> list = sqlheper.getAllUsers_(sql);       
	              int ton=0;
	              EUsers_ u = new EUsers_();
	             for (EUsers_ cn : list) {
	            	 ton=1;
	                 //String log = "Id: "+cn.getID()+" ,Name: " + cn.getUserName() + " ,Pass: " + cn.getPassword()+" ,Chức vụ: " + cn.getRole();
	              // Writing Contacts to log
	                // Log.d("Name: ", log);
	                 u=cn;
	                 
	             }
	             if(ton>0)
	             {
					Intent i = new Intent(getApplicationContext(), MainActivity.class);
					i.putExtra("listUser", u);
					startActivity(i);
	             }
	             else
	             {
	            	 Alertdialog_ al = new Alertdialog_();
	            	 al.alert(R.drawable.tick, "Thông báo", "Tài khoản hoặc mật khẩu không đúng",LoginActivity.this);
	            	
	 			}
	             
			}
		});
    }
}